package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.operate.trading.api.parameter.DispatchedBean;
import com.ymatou.operate.trading.api.service.DispatchedCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.enums.BillTypeEnum;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 发货API请求实体 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Dispatched {

    private static DispatchedBean dispatchedbean;

    private static DispatchedCall dispatchedcall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("卖家发货接口");
    }

    @Before
    public void caseUp() {

        dispatchedbean = new DispatchedBean();
        dispatchedcall = new DispatchedCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(dispatchedcall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_Dispatched_001() {
        Logger.start(true, "卖家发货请求成功");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 支付订单
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            new OrderCallService().acceptOrder(orderId, sellerId);
            Logger.comment("前置数据准备结束");
            // 订单类型
            dispatchedbean.setBillType(BillTypeEnum.UNKNOW);
            // 物流单号
            String DeliverSummary = TestDataManager.randomStr(10);
            dispatchedbean.setDeliverSummary(DeliverSummary);
            // 物流公司类型(0-国际物流 1-国内物流)
            dispatchedbean.setDeliverType("1");
            // 订单ID
            dispatchedbean.setOrderId(orderId);
            // 物流公司
            dispatchedbean.setProvider("洋码头官方合作物流（贝海国际速递）");
            // 卖家ID
            dispatchedbean.setSellerUserId(sellerId);
            // 货物重量
            BigDecimal weight = new BigDecimal(10);
            dispatchedbean.setWeight(weight);
            dispatchedcall.setData(dispatchedbean);
            dispatchedcall.callService();
            Logger.verifyEquals(ECode.SUCCESS, dispatchedcall.getCode(), "验证Code");
            // 数据库验证
            DefaultWapper database = new DefaultWapper();
            HashMap<String, Integer> m = new HashMap<String, Integer>();
            m.put("OrderId", orderId);
            m.put("TradingStatus", 3);
            m.put("UserId", 1113);
            m.put("TraddingId", orderId);
            Logger.verifyEquals(1, database.selectOrder(m).size(), "验证订单表记录");
            Logger.verifyEquals(1, database.selectOrderSummary(m).size(), "验证返货信息表增加发货记录");
            Logger.verifyEquals(1, database.selectTradingStatus(m).size(), "验证交易表记录");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_Dispatched_002() {
        Logger.start(false, "订单不存在");
        try {
            int orderId = 2001501;
            int sellerId = 288484;
            // 订单类型
            dispatchedbean.setBillType(BillTypeEnum.UNKNOW);
            // 物流单号
            String deliverSummary = TestDataManager.randomStr(10);
            dispatchedbean.setDeliverSummary(deliverSummary);
            // 物流公司类型(0-国际物流 1-国内物流)
            dispatchedbean.setDeliverType("1");
            // 订单ID
            dispatchedbean.setOrderId(orderId);
            // 物流公司
            dispatchedbean.setProvider("洋码头官方合作物流（贝海国际速递）");
            // 卖家ID
            dispatchedbean.setSellerUserId(sellerId);
            // 货物重量
            BigDecimal weight = new BigDecimal(10);
            dispatchedbean.setWeight(weight);
            dispatchedcall.setData(dispatchedbean);
            dispatchedcall.callService();
            Logger.verifyEquals(ECode.FAILED201, dispatchedcall.getCode(), "验证Code");
            Logger.verifyIsContains(ECode.FAILED201_MSG_11, dispatchedcall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_Dispatched_003() {
        Logger.start(false, "当前订单状态不能发货");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 支付订单
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            Logger.comment("前置数据准备结束");
            // 接单
            /* new OrderCallService().acceptOrder(OrderId, SellerId); */
            // 订单类型
            dispatchedbean.setBillType(BillTypeEnum.UNKNOW);
            // 物流单号
            String deliverSummary = TestDataManager.randomStr(10);
            dispatchedbean.setDeliverSummary(deliverSummary);
            // 物流公司类型(0-国际物流 1-国内物流)
            dispatchedbean.setDeliverType("1");
            // 订单ID
            dispatchedbean.setOrderId(orderId);
            // 物流公司
            dispatchedbean.setProvider("洋码头官方合作物流（贝海国际速递）");
            // 卖家ID
            dispatchedbean.setSellerUserId(sellerId);
            // 货物重量
            BigDecimal weight = new BigDecimal(10);
            dispatchedbean.setWeight(weight);
            dispatchedcall.setData(dispatchedbean);
            dispatchedcall.callService();
            Logger.verifyEquals(ECode.FAILED201, dispatchedcall.getCode(), "验证Code");
            Logger.verifyEquals(true, dispatchedcall.getMsg().contains(ECode.FAILED201_MSG_30), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_Dispatched_004() {
        Logger.start(false, "订单为null");
        try {
            int orderId = 2001501;
            int sellerId = 288484;
            // 订单类型
            dispatchedbean.setBillType(BillTypeEnum.UNKNOW);
            // 物流单号
            String deliverSummary = TestDataManager.randomStr(10);
            dispatchedbean.setDeliverSummary(deliverSummary);
            // 物流公司类型(0-国际物流 1-国内物流)
            dispatchedbean.setDeliverType("1");
            // 订单ID
            // dispatchedbean.setOrderId(orderId);
            // 物流公司
            dispatchedbean.setProvider("洋码头官方合作物流（贝海国际速递）");
            // 卖家ID
            dispatchedbean.setSellerUserId(sellerId);
            // 货物重量
            BigDecimal weight = new BigDecimal(10);
            dispatchedbean.setWeight(weight);
            dispatchedcall.setData(dispatchedbean);
            dispatchedcall.callService();
            Logger.verifyEquals(ECode.ERROR400, dispatchedcall.getCode(), "验证Code");
            Logger.verifyIsContains(ECode.ERROR400_MSG_9, dispatchedcall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_Dispatched_005() {
        Logger.start(false, "操作id为null");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 支付订单
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            new OrderCallService().acceptOrder(orderId, sellerId);
            Logger.comment("前置数据准备结束");
            // 订单类型
            dispatchedbean.setBillType(BillTypeEnum.UNKNOW);
            // 物流单号
            String deliverSummary = TestDataManager.randomStr(10);
            dispatchedbean.setDeliverSummary(deliverSummary);
            // 物流公司类型(0-国际物流 1-国内物流)
            dispatchedbean.setDeliverType("1");
            // 订单ID
            dispatchedbean.setOrderId(orderId);
            // 物流公司
            dispatchedbean.setProvider("洋码头官方合作物流（贝海国际速递）");
            // 卖家ID
            // dispatchedbean.setSellerUserId(sellerId);
            // 货物重量
            BigDecimal weight = new BigDecimal(10);
            dispatchedbean.setWeight(weight);
            dispatchedcall.setData(dispatchedbean);
            dispatchedcall.callService();
            Logger.verifyEquals(ECode.SUCCESS, dispatchedcall.getCode(), "验证Code");
            // 数据库验证
            DefaultWapper database = new DefaultWapper();
            HashMap<String, Integer> m = new HashMap<String, Integer>();
            m.put("OrderId", orderId);
            m.put("TradingStatus", 3);
            m.put("UserId", 1113);
            m.put("TraddingId", orderId);
            Logger.verifyEquals(1, database.selectOrder(m).size(), "验证订单表记录");
            Logger.verifyEquals(1, database.selectOrderSummary(m).size(), "验证返货信息表增加发货记录");
            Logger.verifyEquals(1, database.selectTradingStatus(m).size(), "验证交易表记录");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    @Ignore
    public void Tc_Dispatched_006() {
        Logger.start(false, "当前订单为退货退款中,不能发货");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            OrderCallService orderService = new OrderCallService();
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 支付订单
            orderService.orderPayRequest(userId, totalPrice, tradingId);
            Logger.comment("前置数据准备结束");
            // 退货退款同步
            orderService.applySalesRefundRequestInJava(orderId);
            Logger.comment("前置数据准备结束");
            dispatchedbean.setBillType(BillTypeEnum.UNKNOW);
            // 物流单号
            String deliverSummary = TestDataManager.randomStr(10);
            dispatchedbean.setDeliverSummary(deliverSummary);
            // 物流公司类型(0-国际物流 1-国内物流)
            dispatchedbean.setDeliverType("1");
            // 订单ID
            dispatchedbean.setOrderId(orderId);
            // 物流公司
            dispatchedbean.setProvider("洋码头官方合作物流（贝海国际速递）");
            // 卖家ID
            dispatchedbean.setSellerUserId(sellerId);
            // 货物重量
            BigDecimal weight = new BigDecimal(10);
            dispatchedbean.setWeight(weight);
            dispatchedcall.setData(dispatchedbean);
            dispatchedcall.callService();
            Logger.verifyEquals(ECode.FAILED201, dispatchedcall.getCode(), "验证Code");
            Logger.verifyEquals(ECode.ErrStatusNotMatch, dispatchedcall.getString("BCode"), "验证BCode");
            Logger.verifyEquals(ECode.FAILED201_MSG_62, dispatchedcall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_Dispatched_007() {
        Logger.start(false, "该订单已发货,重复发货失败");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            OrderCallService orderService = new OrderCallService();
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 支付订单
            orderService.orderPayRequest(userId, totalPrice, tradingId);
            Logger.comment("前置数据准备结束");
            // 接单
            orderService.acceptOrder(orderId, sellerId);
            // 商家发货
            orderService.dispatched(orderId, sellerId);
            Logger.comment("前置数据准备结束");
            dispatchedbean.setBillType(BillTypeEnum.UNKNOW);
            // 物流单号
            String deliverSummary = TestDataManager.randomStr(10);
            dispatchedbean.setDeliverSummary(deliverSummary);
            // 物流公司类型(0-国际物流 1-国内物流)
            dispatchedbean.setDeliverType("1");
            // 订单ID
            dispatchedbean.setOrderId(orderId);
            // 物流公司
            dispatchedbean.setProvider("洋码头官方合作物流（贝海国际速递）");
            // 卖家ID
            dispatchedbean.setSellerUserId(sellerId);
            // 货物重量
            BigDecimal weight = new BigDecimal(10);
            dispatchedbean.setWeight(weight);
            dispatchedcall.setData(dispatchedbean);
            dispatchedcall.callService();
            Logger.verifyEquals(ECode.FAILED201, dispatchedcall.getCode(), "验证Code");
            Logger.verifyEquals("106", dispatchedcall.getString("BCode"), "验证BCode");
            Logger.verifyEquals(orderId + "订单已发货，无需再次发货", dispatchedcall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_Dispatched_008() {
        Logger.start(true, "订单物流类型为贝海直邮,卖家发货成功");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            Map map = new OrderCallService().createM2cOrder(LogisticsTypeEnum.SailProtect);
            int orderId = (Integer) map.get("orderId");
            int sellerId = (Integer) map.get("sellerId");
            int userId = (Integer) map.get("userId");
            int tradingId = (Integer) map.get("tradingId");
            BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
            // 支付订单
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            new OrderCallService().acceptOrder(orderId, sellerId);
            Logger.comment("前置数据准备结束");
            // 订单类型
            dispatchedbean.setBillType(BillTypeEnum.UNKNOW);
            // 物流单号
            String DeliverSummary = TestDataManager.randomStr(10);
            dispatchedbean.setDeliverSummary(DeliverSummary);
            // 物流公司类型(0-国际物流 1-国内物流)
            dispatchedbean.setDeliverType("1");
            // 订单ID
            dispatchedbean.setOrderId(orderId);
            // 物流公司
            dispatchedbean.setProvider("洋码头官方合作物流（贝海国际速递）");
            // 卖家ID
            dispatchedbean.setSellerUserId(sellerId);
            // 货物重量
            BigDecimal weight = new BigDecimal(10);
            dispatchedbean.setWeight(weight);
            dispatchedbean.setIsShippedByXlobo(true);
            dispatchedcall.setData(dispatchedbean);
            dispatchedcall.callService();
            Logger.verifyEquals(ECode.SUCCESS, dispatchedcall.getCode(), "验证Code");
            // 数据库验证
            DefaultWapper database = new DefaultWapper();
            HashMap<String, Integer> m = new HashMap<String, Integer>();
            m.put("OrderId", orderId);
            m.put("TradingStatus", 3);
            m.put("UserId", 1113);
            m.put("TraddingId", orderId);
            Logger.verifyEquals(1, database.selectOrder(m).size(), "验证订单表记录");
            Logger.verifyEquals(1, database.selectOrderSummary(m).size(), "验证返货信息表增加发货记录");
            Logger.verifyEquals(1, database.selectTradingStatus(m).size(), "验证交易表记录");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }


}
